<template>
    <div class="s-tabbar row items-center" :style="{ height: height, background: background }">
        <div
            class="col column flex-center"
            v-for="(item, index) in list"
            :key="index"
            @click="$emit('change', item)"
            @touchstart="touchstart(item)"
            @touchend="touchend"
            :style="{
                height: height,
                transform: conf.transformItem.value === item.value ? conf.transform : '',
                transition: conf.transition
            }"
        >
            <div class="flex justify-center relative" style="height: 20px; width: 20px; align-items: inherit">
                <s-icon
                    class="absolute flex inline"
                    :type="item.icon"
                    :color="item.active ? colorActive : color"
                    :size="item.iconSize ?? iconSize"
                    style="bottom: 0px"
                    :style="item.iconStyle"
                />
            </div>
            <div :style="{ fontSize: fontSize, color: item.active ? colorActive : color }" style="margin-top: 2px">
                {{ item.label }}
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { watch } from 'vue'
import Timer from '~/utils/Timer'

interface tabbarItem {
    icon: any
    iconSize?: string
    iconStyle?: any
    active?: boolean
    value?: string
    label: string
}

const props = defineProps({
    /**
     * 高度
     */
    height: { default: 'var(--s-tabbar-height)' },
    /**
     * 背景色
     */
    background: { default: 'var(--s-tabbar-bg)' },
    /**
     * 列表
     */
    list: { default: [] as tabbarItem[] },
    /**
     * 默认显示颜色
     */
    color: { default: '#000000' },
    /**
     * 选中显示颜色
     */
    colorActive: { default: '#e1251b' },
    /**
     * 图标大小
     */
    iconSize: { default: '40' },
    /**
     * 字体大小
     */
    fontSize: { default: '22rpx' },
    /**
     * 当前选中下标
     */
    active: { default: null as any },
    /**
     * 动画效果
     */
    ani: { default: null as any }
})

props.list.map((_item) => {
    if (_item.value == undefined) _item.value = _item.label
})

const initActive = () => {
    if (props.list.length > 0) {
        props.list.map((_item, index) => {
            if (props.active) {
                _item.active = props.active === _item.value
            } else {
                _item.active = index === 0
            }
        })
    }
}

watch(
    () => props.active,
    () => {
        initActive()
    }
)

initActive()

const conf = reactive({
    transformItem: {} as tabbarItem,
    transform: '',
    transition: 'all .3s',
    timer1: null as any,
    timer2: null as any
})

const touchstart = (item: tabbarItem) => {
    if (props.ani == null) return
    Timer.unOnce(conf.timer1)
    Timer.unOnce(conf.timer2)
    conf.transformItem = item
    conf.transition = 'all 0s'
    Timer.once(() => {
        conf.transition = 'all .3s'
        conf.transform = 'scale(0.85)'
    }, 10)
}

const touchend = () => {
    if (props.ani == null) return
    conf.timer1 = Timer.once(() => {
        conf.transform = 'scale(1.15)'
        conf.timer2 = Timer.once(() => {
            conf.transform = 'scale(1)'
        }, 300)
    }, 300)
}
</script>
<style lang="scss" scoped>
.s-tabbar {
    position: fixed;
    z-index: 98;
    bottom: 0;
    left: 0;
    width: 100vw;
    box-shadow: 0px 6rpx 20rpx #c7c7c7;
}
</style>
